﻿#region 命名空间

using System;
using System.Text;
using WinnerCMS.BLL;
using WinnerCMS.BLL.Shop;
using WinnerCMS.Common;
using WinnerCMS.Model;
using WinnerCMS.Model.Attribute;
using WinnerCMS.Model.Enum;
using WinnerCMS.WebControls;
using WinnerCMS.WebFactory;

#endregion

namespace WinnerCMS.WebUI.Admin.Shop
{
    [Access("ConfirmOrder,CancelOrder,DelOrder")]
    public partial class Order : PageBase
    {
        private ModelType BaseModel;

        /// <summary>
        ///     内容名称
        /// </summary>
        private string ContentName;

        /// <summary>
        ///     模型ID
        /// </summary>
        private byte _modelId;

        private readonly OrdersManage _myBLL = new OrdersManage();

        #region 网页属性

        /// <summary>
        ///     是否处于搜索状态
        /// </summary>
        private bool IsSearch
        {
            get
            {
                if (ViewState["IsSearch"] != null)
                {
                    return Convert.ToBoolean(ViewState["IsSearch"]);
                }
                return false;
            }
            set { ViewState["IsSearch"] = value; }
        }

        #endregion

        protected void Page_Load(object sender, EventArgs e)
        {
            var b = byte.TryParse(Request["ModelId"], out this._modelId);
            if (b == false)
            {
                ErrClass.Redirect("缺少 ModelId 参数。");
            }
            this.ContentName = ModelManage.GetContentName(this._modelId);
            this.BaseModel = ModelManage.GetBaseModel(this._modelId);

            if (!IsPostBack)
            {
                BindList(1);
            }
        }

        #region 绑定数据

        /// <summary>
        ///     绑定数据
        /// </summary>
        private void BindList(int PageNo)
        {
            this.GV_List.EmptyDataTemplate = GetTemplate(this);

            var strPath = string.Format("{0} → <a href='Order.aspx?ModelId={1}'>订单管理</a>",
                ModelManage.GetModelName(this._modelId), this._modelId
                );


            this.lblPath.Text = strPath;

            var pp = new PagingParam();
            pp.PageNo = PageNo;
            pp.PageSize = this.PageNavigation1.PageSize;
            this.GV_List.DataSource = this._myBLL.BindList(this.ddlSort.SelectedValue, this.txtKeyWord.Text.Trim(),
                this.ddlStatus.SelectedValue, this.ddlDeliverStatus.SelectedValue, pp);
            this.GV_List.DataBind();
            this.PageNavigation1.RecordCount = pp.RecordCount;

            if (this.GV_List.Rows.Count <= 0)
            {
            }
        }

        #endregion

        #region 动态修改链接

        /// <summary>
        ///     动态修改链接
        /// </summary>
        /// <param name="ContentTitle"></param>
        /// <param name="intLong"></param>
        /// <param name="ID"></param>
        /// <param name="Hits"></param>
        /// <returns></returns>
        protected string MyLink(object ContentTitle, object OrderId)
        {
            var winner = new StringBuilder();
            winner.Append(" <a href=\"#\" onclick=\"");
            //if (ContenEdit)
            {
                winner.Append(string.Concat("return NewTabPage('", ContentTitle, "','", "Shop/OrderList.aspx?OrderId=",
                    OrderId, "');"));
            }
            //else
            //{
            //    winner.Append("alert ('您缺少修改权限');return false;\"");
            //}


            winner.AppendFormat(" \">{0}</a>", ContentTitle);


            return winner.ToString();
        }

        #endregion

        protected string Status(object value)
        {
            var status = Convert.ToInt32(value);


            return this._myBLL.GetOrderStatusDescription((Status) status);
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            IsSearch = true;
            BindList(1);
        }

        protected void btnDelete_Click(object sender, EventArgs e)
        {
            this._myBLL.Delete(GetSelectedRow.GetIDs(this.GV_List), this.UpdatePanel1);

            BindList(1);
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            var IDs = GetSelectedRow.GetIDs(this.GV_List);
            foreach (var id in IDs.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries))
            {
                this._myBLL.UpdateOrderStatus(int.Parse(id), BLL.Shop.Status.WaitForConfirm);
            }

            BindList(this.PageNavigation1.PageNo);
        }

        protected void btnConfirmed_Click(object sender, EventArgs e)
        {
            var IDs = GetSelectedRow.GetIDs(this.GV_List);
            foreach (var id in IDs.Split(new[] {','}, StringSplitOptions.RemoveEmptyEntries))
            {
                this._myBLL.UpdateOrderStatus(int.Parse(id), BLL.Shop.Status.Confimed);
            }

            BindList(this.PageNavigation1.PageNo);
        }

        protected void PageNavigation1_PageChanging(object src, PageChangingEventArgs e)
        {
            BindList(e.NewPageIndex);
        }
    }
}